package ru.ibrae.mchs.radiation.common.excelEntities;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class ExcelSubject {

    private String name;
    private ArrayList<ExcelRajon> rajons = new ArrayList<ExcelRajon>();
    private ArrayList<ExcelOldRajon> oldRajons = new ArrayList<ExcelOldRajon>();

    public ExcelSubject(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void doDeepSort() {
        if (!rajons.isEmpty()) {
            Collections.sort(getRajons(), new Comparator<ExcelRajon>() {

                @Override
                public int compare(ExcelRajon o1, ExcelRajon o2) {
                    return o1.getSortFactor().compareToIgnoreCase(o2.getSortFactor());
                }
            });
            for (ExcelRajon excelRajon : getRajons()) {
                excelRajon.doDeepSort();
            }
        } else {
            Collections.sort(getOldRajons(), new Comparator<ExcelOldRajon>() {

                @Override
                public int compare(ExcelOldRajon o1, ExcelOldRajon o2) {
                    return o1.getName().compareToIgnoreCase(o2.getName());
                }
            });
            for (ExcelOldRajon excelOldRajon : getOldRajons()) {
                Collections.sort(excelOldRajon.getExcelGeoobjects(), new Comparator<ExcelGeoobject>() {

                    @Override
                    public int compare(ExcelGeoobject o1, ExcelGeoobject o2) {
                        return o1.getName().compareToIgnoreCase(o2.getName());
                    }
                });
                Collections.sort(excelOldRajon.getExcelSelsovet(), new Comparator<ExcelSelsovet>() {

                    @Override
                    public int compare(ExcelSelsovet o1, ExcelSelsovet o2) {
                        return o1.getSortFactor().compareToIgnoreCase(o2.getSortFactor());
                    }
                });
                for (ExcelSelsovet excelSelsovet : excelOldRajon.getExcelSelsovet()) {
                    Collections.sort(excelSelsovet.getExcelGeoobjects(), new Comparator<ExcelGeoobject>() {

                        @Override
                        public int compare(ExcelGeoobject o1, ExcelGeoobject o2) {
                            return o1.getName().compareToIgnoreCase(o2.getName());
                        }
                    });
                }
            }
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof ExcelSubject) {
            ExcelSubject excelSubject = (ExcelSubject) obj;
            if (this.getName().equalsIgnoreCase(excelSubject.getName())) {
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 83 * hash + (this.name != null ? this.name.hashCode() : 0);
        return hash;
    }

    /**
     * @return the rajons
     */
    public ArrayList<ExcelRajon> getRajons() {
        return rajons;
    }

    /**
     * @return the oldRajons
     */
    public ArrayList<ExcelOldRajon> getOldRajons() {
        return oldRajons;
    }
}
